#!/bin/tcsh -ef
#############################################################################
#                                                                           #
# Title: Normalize Particles                                                #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 16/10/2017                                             #
# Last Modification: 23/10/2017                                             #
# Author...........: Ricardo Righetto                                       #
#                                                                           #
#############################################################################
#
# SORTORDER: 50
#
# MANUAL: This script normalizes a particle stack extracted by Gautomatch
#
# DISPLAY: gautomatch_boxsize
# DISPLAY: relion_radnorm
# DISPLAY: relion_delraw
#
#$end_local_vars
echo "<<@progress: 0>>"
set bin_2dx = ""
set proc_2dx = ""
#
set relion_radnorm = ""
set relion_delraw = ""
set gautomatch_boxsize = ""
set movie_stackname = ""
set import_rawstack = ""
set imagename_original = ""
set raw_gaincorrectedstack = "" 
set gautomatch_number_particles = ""
#
#
#$end_vars
#
set scriptname = normalize_particles
\rm -f LOGS/${scriptname}.results
#
source ${proc_2dx}/initialize
#
if ( ! -d ${dir_relion} ) then
  echo ":: "
  ${proc_2dx}/linblock "RELION installation directory not found. Is it installed ???"
  ${proc_2dx}/linblock "${dir_relion}"
  ${proc_2dx}/linblock "The location of RELION installation is defined in Preferences."
  echo ":: "
else
  set path=(${dir_relion}/bin $path)
endif
#
if ( ${gautomatch_number_particles} == 0 ) then
	echo ":: "
	echo ":: There is no particle stack. Maybe no particles were detected on this micrograph?"
	echo ":: Please check the picking options (e.g. Gautomatch)."
	echo ":: "
	exit
endif

if ( `echo "${relion_radnorm} < 1" | bc` ) then
	set relion_rad = `echo "${relion_radnorm} * ${gautomatch_boxsize}/2" | bc`
else
	set relion_rad = ${relion_radnorm}
endif

set orimic = `echo ${imagename_original} | tr -d '"' | awk -F \/ '{print $NF}' `
set len = `echo ${orimic} | wc -c`
@ len -= 1
if ( ${len} <= 1 ) set orimic = ${raw_gaincorrectedstack}
#set orimic = ${raw_gaincorrectedstack}
#echo ${orimic},${imagename_original},${raw_gaincorrectedstack}
# set orimicbase = `basename ${orimic} .mrc`
set orimicbase = `echo ${import_rawstack} | sed 's/\./\_/g' | sed 's/_mrc//g' | sed 's/_dm4//g' | sed 's/_tif//g' | sed -e 's/\./_/g' -e 's/\,/_/g' -e 's/ /_/g' -e's/:/_/g' -e's/#/_/g'`

# Make sure that the radius is integer:
set relion_rad = `printf %d ${relion_rad}`
echo ":: "
echo ":: Particle will be normalized with a background radius equal to ${relion_rad} pixels."
echo ":: "
echo ":: "
echo "::Running:"
echo ":: "
echo ":: "${dir_relion}/bin/relion_preprocess
echo ":: "--operate_on ${movie_stackname}_particles.mrcs
echo ":: "--norm
echo ":: "--invert_contrast
echo ":: "--bg_radius ${relion_rad}
echo ":: "--operate_out ${movie_stackname}_particles_prep.mrcs
echo ":: "

#
${dir_relion}/bin/relion_preprocess \
--operate_on ${movie_stackname}_particles.mrcs \
--norm \
--invert_contrast \
--bg_radius ${relion_rad} \
--white_dust 3.0 \
--black_dust 3.0 \
--operate_out ${movie_stackname}_particles_prep.mrcs \
#
#mv ${movie_stackname}_particles_prep.mrcs.mrcs ${movie_stackname}_particles_prep.mrcs 
if ( -e ${movie_stackname}_particles_prep.mrcs ) then
  \mv -f ${movie_stackname}_particles_prep.mrcs ${orimicbase}_particles_prep.mrcs
endif

\mv -f ${movie_stackname}_particles_prep.star ${orimicbase}_particles_prep.star
echo "#IMAGE-IMPORTANT: ${orimicbase}_particles_prep.mrcs <Normalized particles>" >> LOGS/${scriptname}.results
echo "#IMAGE: ${orimicbase}_particles_prep.star <Normalized particles STAR file>" >> LOGS/${scriptname}.results
#
ls *mrcs
if ( ${relion_delraw} == "y" ) then
	echo ":: "
	echo ":: relion_delraw = ${relion_delraw}"
	echo ":: WARNING: the original non-normalized particle stack will be deleted now!"
	echo ":: You may have to run the particle picking script again if changing picking/normalization parameters later."
	echo ":: "
	\rm ${movie_stackname}_particles.mrcs
endif
#
echo "<<@progress: 100>>"
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
#
exit
#
